//
//  VideoTableViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "VideoTableViewController.h"
#import "VideoTableViewCell.h"
#import "HelpHandel.h"
#import "VideoDataViewController.h"


#import "MJRefresh.h"
#import "MBProgressHUD.h"


static NSUInteger i = 10;

#define Video @"http://c.3g.163.com/nc/video/list/V9LG4B3A0/y/0-10.html"
#define NewVideo @"http://c.3g.163.com/nc/video/list/V9LG4B3A0/y/%ld-10.html"

@interface VideoTableViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>


@property(nonatomic,retain) NSMutableArray *videoEventsArray;

@property(nonatomic,retain) MBProgressHUD *progressHUD;

@end
static NSString *IdentiferCell = @"cell";
@implementation VideoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[VideoTableViewCell class] forCellReuseIdentifier:IdentiferCell];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self loadProgressHUD];  //小菊花
    self.videoEventsArray = [NSMutableArray array];
    [[HelpHandel shareVideoHandel] requestWithUrl:Video block:^(NSMutableArray *array) {
        self.videoEventsArray = array;
        [self.tableView reloadData];
    }];
    //刷新
    [self lyoutMJRefresh];

}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  //  NSLog(@"%ld",self.videoEventsArray.count);
    return self.videoEventsArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IdentiferCell forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[VideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IdentiferCell];
    }
    [self.progressHUD hide:YES];
    [cell cellWithData:self.videoEventsArray[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoDataViewController *videoDVC = [[VideoDataViewController alloc] init];
    videoDVC.hidesBottomBarWhenPushed = YES;
    videoDVC.videoModel = self.videoEventsArray[indexPath.row];
    [self.navigationController pushViewController:videoDVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270;
}

-(void)layoutSublayers{
    UIView *view = [[UIView alloc] initWithFrame:self.tableView.bounds];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 1;
    view.tag = 900;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];;
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:@"页面加载失败!" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(refreshControl) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    [self.tableView addSubview:view];
}

-(void)refresh{
    
    self.videoEventsArray = [NSMutableArray array];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
         [[HelpHandel shareVideoHandel] requestWithUrl:NewVideo block:^(NSMutableArray *array) {
             i += 10;
             self.videoEventsArray = array;
             [self.tableView reloadData];
             [self.tableView.header endRefreshing];
       }];
    
    });

    if (self.videoEventsArray.count != 0) {
        [[self.view viewWithTag:900] removeFromSuperview];//移除
    }
}

//小菊花
-(void)loadProgressHUD{
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.progressHUD.center = self.view.center;
    self.progressHUD.minSize = CGSizeMake(100, 100);
    self.progressHUD.alpha = 1;
    self.progressHUD.labelText = @"正在拼命加载中...";
    self.progressHUD.square = YES;//设置显示框的高度和宽度一样
    self.progressHUD.frame = self.view.bounds;
    [self.tableView addSubview:self.progressHUD];
    [self.progressHUD show:YES];
}
//刷新
-(void)lyoutMJRefresh{
    //下拉
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReresh)];
    [self.tableView.header beginRefreshing];
    //上拉
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerWithreresh)];
    
}

-(void)headerReresh{
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.videoEventsArray = [NSMutableArray array];
    [[HelpHandel shareVideoHandel] requestWithUrl:Video block:^(NSMutableArray *array) {
        [self.videoEventsArray addObjectsFromArray:array];
        [self.tableView reloadData];
        i = 10;
        [self.tableView.header endRefreshing];
    }];
 
}
-(void)footerWithreresh{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [[HelpHandel shareVideoHandel] requestWithUrl:[NSString stringWithFormat:NewVideo,i] block:^(NSMutableArray *array) {
             [self.videoEventsArray addObjectsFromArray:array];
      
            [self.tableView reloadData];
            i += 10;
             [self.tableView.footer endRefreshing];
            
        }];
   
    });
  
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
     [[NSNotificationCenter defaultCenter] postNotificationName:@"supportedInterfaceOrientations" object:self userInfo:@{@"surport" : [NSNumber numberWithBool:YES]}];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    
      [[NSNotificationCenter defaultCenter] postNotificationName:@"supportedInterfaceOrientations" object:self userInfo:@{@"surport" : [NSNumber numberWithBool:NO]}];
}

@end
